import Vue from 'vue'
import VueRouter from 'vue-router'

// 路由懒加载
const Layout = () => import( /* webpackChunkName: "group-index" */ "@/layout/index.vue")
const Welcome = () => import( /* webpackChunkName: "group-index" */ '@/components/welcome.vue')
const Login = () => import( /* webpackChunkName: "group-index" */ '@/pages/login.vue')

const User = () => import( /* webpackChunkName: "group-role" */ "@/pages/user.vue")
const Rights = () => import( /* webpackChunkName: "group-role" */ "@/pages/rights.vue")
const Roles = () => import( /* webpackChunkName: "group-role" */ "@/pages/roles.vue")

const AddGood = () => import( /* webpackChunkName: "group-good" */ "@/pages/addgood.vue")
const Goods = () => import( /* webpackChunkName: "group-good" */ "@/pages/goods.vue")
const GoodCate = () => import( /* webpackChunkName: "group-good" */ "@/pages/goodcate.vue")

const Param = () => import( /* webpackChunkName: "group-cate" */ "@/pages/param.vue")
const Orders = () => import( /* webpackChunkName: "group-cate" */ "@/pages/orders.vue")
const Reports = () => import( /* webpackChunkName: "group-cate" */ "@/pages/reports.vue")


// 全局使用router
Vue.use(VueRouter)

const routes = [{
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    component: Login
  },
  {
    path: '/home',
    name: "index",
    component: Layout,
    redirect: '/welcome',
    children: [{
        path: '/users',
        name: '用户管理/用户列表',
        component: User
      },
      {
        path: '/goods',
        name: '商品管理/商品列表',
        component: Goods
      },
      {
        path: '/welcome',
        component: Welcome
      },
      {
        path: '/roles',
        name: '权限管理/权限列表',
        component: Roles
      },
      {
        path: '/rights',
        name: '权限管理/角色列表',
        component: Rights
      },
      {
        path: '/params',
        name: '商品管理/分类参数',
        component: Param
      },
      {
        path: '/categories',
        name: '商品管理/商品分类',
        component: GoodCate
      },
      {
        path: '/orders',
        name: '订单管理/订单列表',
        component: Orders
      },
      {
        path: '/reports',
        name: '数据统计/数据报表',
        component: Reports
      },
      {
        path: '/addgood',
        name: '商品管理/商品列表/增加商品',
        component: AddGood
      },
    ]
  }
]

const router = new VueRouter({
  routes
})

// 全局前置守卫
router.beforeEach((to, from, next) => {
  if (to.path == '/login') next()

  // 获取token
  const tokenUser = window.sessionStorage.getItem('token')
  if (!tokenUser && to.path !== '/login') return next('/login')
  next()
})

export default router